package com.bjkcst.hcnet;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.TextureView;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Guideline;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: glc.
 * Date: 2022/5/13  16:19
 * Description:
 */
public class CameraPreviewView extends ConstraintLayout {

    private Context mContext;

    private int mPreviewWindowNumber = 0;

    private final static List<RelativeLayout> mPreviewWindowsList = new ArrayList<>();

    private final Map<String,TextureView> mAllCameraTextureview = new HashMap<>();

    private final List<String> mCurPreviewCameraNo = new ArrayList<>();

    public CameraPreviewView(@NonNull Context context) {
        this(context, null);
    }

    public CameraPreviewView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CameraPreviewView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {

    }


    /**
     * 设置默认显示窗口个数
     */
    public void defPreviewNum(int d,int layout) {
        mPreviewWindowsList.clear();
        this.mPreviewWindowNumber = d;
        List<LayoutParams> params = getParams(d,layout);

        for (int i = 0; i < d; i++) {
            RelativeLayout relativeLayout = new RelativeLayout(mContext);
            relativeLayout.setLayoutParams(params.get(i));
//            relativeLayout.setBackgroundResource(R.drawable.shape_52ffff_bg);
            addView(relativeLayout);

            mPreviewWindowsList.add(relativeLayout);
        }
    }


    private List<LayoutParams> getParams(int d,int layout) {
        List<LayoutParams> paramsList = new ArrayList<>();
        if (d == 1) {
            LayoutParams layoutParams1 = new LayoutParams(
                    0, LayoutParams.MATCH_PARENT);
            layoutParams1.topToTop = getId();
            layoutParams1.leftToLeft = getId();
            layoutParams1.rightToRight = getId();
            paramsList.add(layoutParams1);

        } else if (d == 2) {
            //TODO 待维护修改
            Guideline guideline2 = new Guideline(mContext);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,5);
            guideline2.setLayoutParams(layoutParams);
            guideline2.setId(View.generateViewId());
            guideline2.setBackgroundColor(Color.BLUE);
            layoutParams.orientation = 0;
            addView(guideline2);
            guideline2.setGuidelinePercent(0.5F);
            if (layout == 1){
                LayoutParams layoutParams2_1 = new LayoutParams(
                        LayoutParams.MATCH_PARENT,442 );
//                layoutParams2_1.rightToLeft = getId();
                layoutParams2_1.topToTop = getId();
                layoutParams2_1.leftToLeft = getId();
//                layoutParams2_1.rightMargin = 10;
//                layoutParams2_1.leftMargin = 20;
                paramsList.add(layoutParams2_1);

                LayoutParams layoutParams2_2 = new LayoutParams(
                        LayoutParams.MATCH_PARENT,442 );
//                layoutParams2_2.leftToRight = getId();
                layoutParams2_2.topToBottom = guideline2.getId();
                layoutParams2_2.rightToRight = getId();
//                layoutParams2_2.leftMargin = 10;
                layoutParams2_2.topMargin =10;
//                layoutParams2_2.rightMargin = 20;
                paramsList.add(layoutParams2_2);
            }else {
                LayoutParams layoutParams2_1 = new LayoutParams(
                        0, LayoutParams.MATCH_PARENT);
                layoutParams2_1.rightToLeft = guideline2.getId();
                layoutParams2_1.topToTop = getId();
                layoutParams2_1.leftToLeft = getId();
                layoutParams2_1.rightMargin = 10;
                layoutParams2_1.leftMargin = 20;
                paramsList.add(layoutParams2_1);

                LayoutParams layoutParams2_2 = new LayoutParams(
                        0, LayoutParams.MATCH_PARENT);
                layoutParams2_2.leftToRight = guideline2.getId();
                layoutParams2_2.topToTop = getId();
                layoutParams2_2.rightToRight = getId();
                layoutParams2_2.leftMargin = 10;
                layoutParams2_2.rightMargin = 20;
                paramsList.add(layoutParams2_2);
            }
        } else if (d == 3) {
            Guideline guideline3_1 = new Guideline(mContext);
            guideline3_1.setId(View.generateViewId());
            guideline3_1.setGuidelinePercent(0.33f);
            addView(guideline3_1);

            LayoutParams layoutParams3_1 = new LayoutParams(
                    LayoutParams.MATCH_CONSTRAINT, LayoutParams.MATCH_CONSTRAINT);
            layoutParams3_1.rightToLeft = guideline3_1.getId();
            paramsList.add(layoutParams3_1);
            paramsList.add(layoutParams3_1);

            Guideline guideline3_2 = new Guideline(mContext);
            guideline3_2.setId(View.generateViewId());
            guideline3_2.setGuidelinePercent(0.33f);
            addView(guideline3_2);

            LayoutParams layoutParams3_2 = new LayoutParams(
                    LayoutParams.MATCH_CONSTRAINT, LayoutParams.MATCH_CONSTRAINT);
            layoutParams3_2.rightToLeft = guideline3_2.getId();
            layoutParams3_2.leftToRight = guideline3_1.getId();
            paramsList.add(layoutParams3_2);
            paramsList.add(layoutParams3_1);

            LayoutParams layoutParams3_3 = new LayoutParams(
                    LayoutParams.MATCH_CONSTRAINT, LayoutParams.MATCH_CONSTRAINT);
            layoutParams3_3.leftToRight = guideline3_2.getId();
            paramsList.add(layoutParams3_2);
            paramsList.add(layoutParams3_1);
        }
        return paramsList;
    }


    @Deprecated
    public void showPreview(String... deviceNo) throws InterruptedException {
        if (mPreviewWindowNumber != deviceNo.length) {
            throw new SecurityException("----------");
        }
        for (int i = 0; i < deviceNo.length; i++) {
            RelativeLayout relativeLayout = mPreviewWindowsList.get(i);
            relativeLayout.removeAllViews();
            CameraBean camera = CameraManager.getInstance().getCameraBeanByDeviceNo(deviceNo[i]);
            CustomSurfaceTexture customSurfaceTexture = new CustomSurfaceTexture(camera.deviceNo);
            TextureView textureView = new TextureView(mContext);
            textureView.setSurfaceTexture(customSurfaceTexture);
            camera.surfaceTexture = customSurfaceTexture;
            relativeLayout.addView(textureView);
        }
        CameraManager.getInstance().previewDeivce(deviceNo);
    }
    @Deprecated
    public void switchPreviewWindow(String... deviceNo) throws InterruptedException {
        CameraManager.getInstance().switchPreview();
        showPreview(deviceNo);
    }


    /**
     * 开始预览  开始加载登录成功的所有设备，采用掩藏的方式进行摄像头的切换
     * @param list 相机编号
     */
    public void initCameraPreview(List<CameraBean> list,String[] preview1,String[] preview2){
        for (CameraBean cameraBean : list) {
            int deviceNo = cameraBean.deviceNo;
            String deviceNoStr = String.valueOf(deviceNo);
            CameraBean camera = CameraManager.getInstance().getCameraBeanByDeviceNo(deviceNoStr);
            CustomSurfaceTexture customSurfaceTexture = new CustomSurfaceTexture(deviceNo);
            TextureView textureView = new TextureView(mContext);
            textureView.setSurfaceTexture(customSurfaceTexture);
            camera.surfaceTexture = customSurfaceTexture;
            mAllCameraTextureview.put(deviceNoStr,textureView);
        }
        for (int i = 0; i < preview1.length; i++) {
            RelativeLayout relativeLayout = mPreviewWindowsList.get(i);
            relativeLayout.addView(mAllCameraTextureview.get(preview1[i]));
        }
        for (int i = 0; i < preview2.length; i++) {
            RelativeLayout relativeLayout = mPreviewWindowsList.get(i);
            TextureView textureView = mAllCameraTextureview.get(preview2[i]);
            assert textureView != null;
            textureView.setVisibility(View.GONE);
            relativeLayout.addView(textureView);
        }
        mCurPreviewCameraNo.addAll(Arrays.asList(preview1));
    }

    public void cameraPreview(String[] deviceNoAar){
        for (String deviceNo : mCurPreviewCameraNo) {
            mAllCameraTextureview.get(deviceNo).setVisibility(GONE);
        }
        mCurPreviewCameraNo.clear();
        for (String deviceNo : deviceNoAar) {
            mAllCameraTextureview.get(deviceNo).setVisibility(View.VISIBLE);
        }
        mCurPreviewCameraNo.addAll(Arrays.asList(deviceNoAar));

    }

}